/*
* @Author: sam
* @Date:   2017-10-18 13:24:37
* @Last Modified by:   sam
* @Last Modified time: 2017-10-30 15:59:26
* @Email: 83742368@qq.com
* @GitHub : https://github.com/AllanBian
*/
(function(win){
	var gaodeMap,tipBox,typeBox;
	var colors = Utils.color();
	var indexedDB;
	var zoom = {
		'shi' : 8,
		'xian' : 9,
		'zhen' : 10,
		'cun' : 12,
		'one' : 12
	};
	gaodeMap = new Class.Map("gaodeMap");
	tipBox = new Class.TipBox("tipBox");//内容介绍容器
	typeBox = new Class.TypeBox("typeBox");
	indexedDB = Utils.indexedDB();

	//发送获取版本的ajax
	getVersion();

	//获取版本
	function getVersion(){
		var mapVersion = Utils.getLocalStorage('Map_Version');
		Utils.ajax({
			type : 'get',
			url : Configs.api.getVersion,
			data : {VersionType : 1},
			beforeSend : function(){
				//loading.classList.add('show');
			},
			afterSend : function(){
				//loading.classList.remove('show');
			},
			success : function(json){
				var data = JSON.parse(json);
				var chearTag = false;
				var verson = "";
				verson = data.data.VersionCode;
				if(mapVersion !== data.data.VersionNo){//地图数据不一样,清除db
					Utils.setLocalStorage('Map_Version',data.data.VersionNo);
					chearTag = true;
				}
				if(indexedDB){
					indexedDB.openDB("",verson,startDB,chearTag);
				}else{
					drawAjax();
				}
			}
		});
	}

	//开始使用db
	function startDB(){
		indexedDB.findData(typeBox.status+gaodeMap.activityCode,null,function(countylist){
			//使用db数据
			drawFun(countylist,null);
		},function(){
			//发送ajax
			drawAjax();
		});
	}

	//双击点击覆盖区域
	function enterPolygon(polygon,event){
		var tag;
		var content = {},obj = {},centerObj = {},xian = {};
		tag = typeBox.next();
		if(tag && gaodeMap.canClick){
			centerObj = {
				zoom : zoom[typeBox.status],
				lng : event.lnglat.lng,
				lat : event.lnglat.lat
			};
			if(typeBox.status === 'one'){
				gaodeMap.changeClickStatus(false);
				gaodeMap.hideOverlays();
				//绘制覆盖物
				obj = gaodeMap.stores[gaodeMap.prefix + polygon.code]['data'];
				xian = gaodeMap.stores[gaodeMap.prefix + gaodeMap.activitySendCode['cun']]['data'];
				gaodeMap.drawPolygon(obj);
				gaodeMap.setZoonAndCenterByType(typeBox.status,centerObj);
				//保存当前显示的覆盖物code,便于实现隐藏所有当前显示的覆盖物
				gaodeMap.setOverlays(polygon.code);

				//装载tipBox数据
				content['tipTitle'] = xian['nameDetail'] + obj['name'];
				content['tipMsg'] = obj['ImfoDetail'];
				content['contact'] = obj['tel'];
				content['address'] = obj['name'];
				content['detail'] = './detail.html?code=' + obj['code'];
				content['lng'] = obj['lng'];
				content['lat'] = obj['lat'];
				content['status'] = typeBox.status;
				content['code'] = obj['code'];

				tipBox.changeContent(content);
				gaodeMap.saveActivitySendCode(typeBox.status,obj['code']);

				window.setTimeout(function(){
					gaodeMap.changeClickStatus(true);
				},500);
			}else{
				if(indexedDB){
					indexedDB.findData(typeBox.status+gaodeMap.activityCode,null,function(countylist){
						//使用db数据
						drawFun(countylist,centerObj);
					},function(){
						//发送ajax
						drawAjax(centerObj);
					});
				}else{
					drawAjax(centerObj);
				}
			}
		}
	}

	//单击点击区域
	function polygonClick(map,polygon,event){
		enterPolygon(polygon,event);
	}

	//点击地图的事件
	function mapClickFun(map){
		var tag;
		tag = typeBox.prev();
		if(tag && gaodeMap.canClick){
			gaodeMap.setActivity(gaodeMap.activitySendCode[typeBox.status]);
			//drawAjax();
			if(indexedDB){
				indexedDB.findData(typeBox.status+gaodeMap.activityCode,null,function(countylist){
					//使用db数据
					drawFun(countylist);
					gaodeMap.setZoonAndCenterByType(typeBox.status);
				},function(){
					//发送ajax
					drawAjax();
				});
			}else{
				drawAjax();
			}
		}
	}
	var loading = document.querySelector('.loading');

	//ajax获取数据绘图
	function drawAjax(centerObj){
		gaodeMap.changeClickStatus(false);

		Utils.ajax({
				type : 'get',
				url : Configs.api.getCity,
				data : { code : gaodeMap.activityCode, types : typeBox.status},
				beforeSend : function(){
					loading.classList.add('show');
				},
				afterSend : function(){
					loading.classList.remove('show');
				},
				success : function(json){
					var data = JSON.parse(json);
					var countylist;
					if(data.status === Configs.status.success){
						countylist = data.data.countylist;
						if(countylist.length > 0){
							//写入数据库
							if(indexedDB){
								indexedDB.addData(typeBox.status+gaodeMap.activityCode,countylist);
							}
							drawFun(countylist,centerObj);
						}
						window.setTimeout(function(){
							gaodeMap.changeClickStatus(true);
						},500);
					}

					if(data.status === Configs.status.empty){
						window.setTimeout(function(){
							gaodeMap.changeClickStatus(true);
						},500);
						console.log('暂无数据');
					}
					// console.log(data);
					console.log(gaodeMap);
					//console.log(typeBox);
				}
		});
	}


	function drawFun(countylist,centerObj){
		var obj = {},content = {};
		var cunArr = [],xiangObj = {};
		//隐藏所有覆盖物
		gaodeMap.hideOverlays();
		//画地图
		for(var i = 0; i < countylist.length; i++){
			//装载每个覆盖物的数据
			obj = {};
			obj['lng'] = countylist[i]['Longtude'];
			obj['lat'] = countylist[i]['Latitude'];
			obj['zoom'] = zoom[typeBox.status];
			obj['name'] = countylist[i]['Name'];
			obj['nameDetail'] = countylist[i]['NameDeatail'];
			obj['ImfoDetail'] = countylist[i]['ImfoDetail'];
			obj['code'] = countylist[i]['Code'];
			obj['data'] = countylist[i]['Ploygnp'];
			obj['color'] = colors[i%colors.length];
			obj['tel'] = '13861737911';
			obj['fnclick'] = polygonClick;
			//装载tipBox数据
			content['tipTitle'] = countylist[i]['NameDeatail'];
			content['tipMsg'] = countylist[i]['Name'] + '相关消息';
			content['contact'] = '13861737911';
			content['address'] = countylist[i]['Name'];
			content['detail'] = './detail.html';
			content['lng'] = countylist[i]['Longtude'];
			content['lat'] = countylist[i]['Latitude'];
			content['status'] = typeBox.status;
			content['code'] = countylist[i]['Code'];

			//如果是市的话设置中心点
			if(typeBox.status === "shi"){
				if(i === 0){
					window.setTimeout(function(){
						if(obj['lng'] !== null){
							gaodeMap.setZoonAndCenterByType(typeBox.status,obj);
						}else{
						}
						//保存最后一次的市,县,镇,村的数据
						gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
					},0);
				}
			}else if(typeBox.status === "xian"){
				gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
			}else if(typeBox.status === "zhen"){
				gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
			}else if(typeBox.status === "cun"){
				cunArr.push(countylist[i]['Name']);
			}else{
				// window.setTimeout(function(){
				// 	tipBox.changeContent(content);
				// 	gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
				// },0);
			}

			//绘制覆盖物
			gaodeMap.drawPolygon(obj);
			//保存当前显示的覆盖物code,便于实现隐藏所有当前显示的覆盖物
			gaodeMap.setOverlays(countylist[i]['Code']);
		}

		if(typeBox.status === "cun"){
			xiangObj = gaodeMap.stores[gaodeMap.prefix + gaodeMap.activityCode]['data'];
			//装载tipBox数据
			content['tipTitle'] = xiangObj['nameDetail'];
			content['tipMsg'] = "下属村有:" + cunArr.join();
			content['contact'] = xiangObj['tel'];
			content['address'] = xiangObj['name'];
			content['detail'] = './list.html?code=' + xiangObj['code'];
			content['lng'] = xiangObj['lng'];
			content['lat'] = xiangObj['lat'];
			content['status'] = typeBox.status;
			content['code'] = xiangObj['code'];
			window.setTimeout(function(){
				tipBox.changeContent(content);
				gaodeMap.saveActivitySendCode(typeBox.status,gaodeMap.activityCode);
			},0);
		}else{
			content['status'] = typeBox.status;
			tipBox.changeContent(content);
		}

		if(typeBox.status !== "shi"){
			gaodeMap.setZoonAndCenterByType(typeBox.status,centerObj);
		}
	}

	// //初始化区域
	//gaodeMap.drawPolygon(polygonObj1);
	gaodeMap.mapClick(mapClickFun);
	//drawAjax();

	Utils.getGeoLocation('geoData');

})(window);

//打电话
function makeCall(dom){
	Utils.makeCall(dom);
}

var listBox = new Class.ListBox('listBox');
//打开app
function openApp(dom){
	var openTypeBox = document.querySelector('.openTypeBox');
	var lng = dom.getAttribute('lng');
	var lat = dom.getAttribute('lat');
	var dest_name = dom.getAttribute('dest_name');
	var dest = {
		'lng' : lng,
		'lat' : lat,
		'dest_name' : dest_name
	};
	var ddDom;
	if(!openTypeBox.classList.contains('bok')){
		openTypeBox.classList.add('bok');
		window.setTimeout(function(){
			openTypeBox.classList.add('show');
			ddDom = document.querySelectorAll('.openType dd');
			for(var i = 0; i < ddDom.length; i++){
			 	(function(n){
			 		ddDom[n].onclick = function(){
			 			listBox.changeMapType(this.getAttribute('type'));
			 			if(openTypeBox.classList.contains('bok')){
			 				openTypeBox.classList.remove('show');
			 				window.setTimeout(function(){
			 					openTypeBox.classList.remove('bok');
			 				},200)
			 			}
			 			listBox.openMapApp(dest);
			 			console.log(listBox);
			 		};
			 	})(i);
			}
			openTypeBox.onclick = function(){
	 			if(openTypeBox.classList.contains('bok')){
	 				openTypeBox.classList.remove('show');
	 				window.setTimeout(function(){
	 					openTypeBox.classList.remove('bok');
	 				},200)
	 			}
			};
		},0);
	}

}